package com.nes.rabbitmq.springboot;

import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;
import org.springframework.amqp.rabbit.annotation.RabbitListenerConfigurer;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.RabbitListenerEndpointRegistrar;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;

/**
 * 注册一个队列 有问题
 */
//@Configuration
//@EnableRabbit
public class RabbitmqConfigure3 implements RabbitListenerConfigurer {

    public static final String QUEUE_NAME = "spring-queue-simple";
    public static final String QUEUE_OBJECT = "spring-queue-object";

    @Bean
    public Queue queue(){
        return new Queue(QUEUE_NAME);
    }

    @Bean
    public Queue queueObject(){
        return new Queue(QUEUE_OBJECT);
    }

    // 以下是发送和接收对象到时需要做的转换-----------------
//    @Bean
//    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
//        RabbitTemplate template = new RabbitTemplate(connectionFactory);
//        template.setMessageConverter(new Jackson2JsonMessageConverter());
//        return template;
//    }

    /*@Bean
    public AmqpTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate template = new RabbitTemplate(connectionFactory);
        template.setMessageConverter(myMessageConverter());

        MessageProperties properties = new MessageProperties();
        properties.setContentType("text/plain");
        properties.setContentEncoding("utf-8");

        DefaultMessagePropertiesConverter propertiesConverter = new DefaultMessagePropertiesConverter();
        propertiesConverter.fromMessageProperties(properties,"utf-8");

        template.setMessagePropertiesConverter(propertiesConverter);
        return template;
    }*/

    @Bean
    public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(myMessageConverter());
        return factory;
    }

//    @Bean
//    public SimpleRabbitListenerContainerFactory myFactory(SimpleRabbitListenerContainerFactoryConfigurer configurer,
//                                                          ConnectionFactory connectionFactory) {
//        SimpleRabbitListenerContainerFactory factory =
//                new SimpleRabbitListenerContainerFactory();
//        configurer.configure(factory, connectionFactory);
//        //factory.setMessageConverter(myMessageConverter());
//        factory.setMessageConverter(myMessageConverter());
//        return factory;
//    }


    @Override
    public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) {
        DefaultMessageHandlerMethodFactory factory = new DefaultMessageHandlerMethodFactory();
        factory.setMessageConverter(new MappingJackson2MessageConverter());
        registrar.setMessageHandlerMethodFactory(factory);
    }

    @Bean
    public MessageConverter myMessageConverter(){
        Jackson2JsonMessageConverter jsonMessageConverter =  new Jackson2JsonMessageConverter();
        ObjectMapper om = JsonHelpers.createObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jsonMessageConverter.setJsonObjectMapper(om);

        return jsonMessageConverter;
    }
}
